package cn.alex.sso.project.controller;

import cn.alex.sso.framework.core.Result;
import cn.hutool.core.date.DateUtil;
import lombok.RequiredArgsConstructor;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * 上传文件
 *
 * @author Alex
 * @date 2024/10/14 下午6:00
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/file")
public class FileController {
    private final FileStorageService fileStorageService;

    /**
     * 上传文件
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile file) {
        Date date = new Date();

        FileInfo info = fileStorageService.of(file)
                .setPath(DateUtil.year(date) + "/" + DateUtil.month(date) + "/" + DateUtil.dayOfMonth(date) + "/")
                .upload();

        return Result.ok(info);
    }
}
